﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kenly.DBFramework.Gateway
{
    /// <summary>
    /// Represents a attribute that contains metadata to describe the association between container and property.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class ManyToManyAssociationAttribute : Attribute
    {
        private Type _elementType;
        private Type _interrelationType;
        private string _interrelationFilter;
        private string _resultOrder = "";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="associationType">AssociationType to reflect the association table.</param>
        public ManyToManyAssociationAttribute(Type associationType)
        {
            _interrelationType = associationType;
        }

        /// <summary>
        /// AssociationType to reflect the association table.
        /// </summary>
        public static AssociationType AssociationType
        {
            get
            {
                return AssociationType.ManyToMany;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Type ElementType
        {
            get
            {
                return _elementType;
            }
            set
            {
                _elementType = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Type InterrelationType
        {
            get
            {
                return _interrelationType;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string InterrelationFilter
        {
            get
            {
                return _interrelationFilter;
            }
            set
            {
                _interrelationFilter = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ResultOrder
        {
            get
            {
                return _resultOrder;
            }
            set
            {
                _resultOrder = value;
            }
        }

        /// <summary>
        /// Converts to an Association.
        /// </summary>
        /// <param name="manyToManyAssociation"></param>
        /// <returns></returns>
        public static implicit operator AssociationAttribute(ManyToManyAssociationAttribute manyToManyAssociation)
        {
            return ToAssociationAttribute(manyToManyAssociation);
        }

        /// <summary>
        /// Converts to an Association.
        /// </summary>
        /// <param name="manyToManyAssociation"></param>
        /// <returns></returns>
        public static AssociationAttribute ToAssociationAttribute(ManyToManyAssociationAttribute manyToManyAssociation)
        {
            AssociationAttribute relationAttribute=new AssociationAttribute(AssociationType.ManyToMany);
            if (manyToManyAssociation == null)
            {
                return relationAttribute;
            }
            relationAttribute.ElementType = manyToManyAssociation.ElementType;
            relationAttribute.InterrelationType=manyToManyAssociation.InterrelationType;
            relationAttribute.InterrelationFilter=manyToManyAssociation.InterrelationFilter;
            relationAttribute.ResultOrder=manyToManyAssociation.ResultOrder;
            return relationAttribute;
        }

    }
}
